/*
 * $Id: QEStateUS.m 34 2009-04-21 20:24:55Z jon@bway.net $
 * Sleeper
 *
 * Created by Jon Gordon on 3/12/08
 * Copyright Jon Gordon.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#import "QEStateUS.h"

#import "QECountry.h"
#import "QESubdivision.h"

static NSArray *allStates = nil;

@implementation QEStateUS

+ (void)initialize
{
    static BOOL initialized = NO;
   //// NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (!initialized) {
        initialized = YES;
       // NSLog(@"  initializing class QEStateUS");
        qeKeyUSStateDesigState     = [[NSString alloc] initWithString:@"state"];
        qeKeyUSStateDesigTerritory = [[NSString alloc] initWithString:@"territory"];
        qeKeyUSStateDesigDistrict  = [[NSString alloc] initWithString:@"special_district"];
        qeKeyUSStateDesigNation    = [[NSString alloc] initWithString:@"nation_in_free_association"];
        qeKeyUSStateDesigTrust     = [[NSString alloc] initWithString:@"trust_territory"];
        qeKeyUSStateDesigMinorI    = [[NSString alloc] initWithString:@"us_minor_outlying_island"];
        
        NSMutableArray *states = [NSMutableArray arrayWithCapacity:69];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Alabama"
                                                postalAbbr:@"AL" designation:qeKeyUSStateDesigState fips52Numeric:@"01" fips52Alpha:@"AL"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Alaska"
                                                postalAbbr:@"AK" designation:qeKeyUSStateDesigState fips52Numeric:@"02" fips52Alpha:@"AK"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"American Samoa"
                                                postalAbbr:@"AS" designation:qeKeyUSStateDesigTerritory fips52Numeric:@"60" fips52Alpha:@"AS"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Arizona"
                                                postalAbbr:@"AZ" designation:qeKeyUSStateDesigState fips52Numeric:@"04" fips52Alpha:@"AZ"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Arkansas"
                                                postalAbbr:@"AR" designation:qeKeyUSStateDesigState fips52Numeric:@"05" fips52Alpha:@"AR"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"California"
                                                postalAbbr:@"CA" designation:qeKeyUSStateDesigState fips52Numeric:@"06" fips52Alpha:@"CA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Colorado"
                                                postalAbbr:@"CO" designation:qeKeyUSStateDesigState fips52Numeric:@"08" fips52Alpha:@"CO"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Connecticut"
                                                postalAbbr:@"CT" designation:qeKeyUSStateDesigState fips52Numeric:@"09" fips52Alpha:@"CT"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Delaware"
                                                postalAbbr:@"DE" designation:qeKeyUSStateDesigState fips52Numeric:@"10" fips52Alpha:@"DE"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"District of Columbia"
                                                postalAbbr:@"DC" designation:qeKeyUSStateDesigDistrict fips52Numeric:@"11" fips52Alpha:@"DC"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Federated States of Micronesia"
                                                postalAbbr:@"FM" designation:qeKeyUSStateDesigNation fips52Numeric:@"64" fips52Alpha:@"FM"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Florida"
                                                postalAbbr:@"FL" designation:qeKeyUSStateDesigState fips52Numeric:@"12" fips52Alpha:@"FL"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Georgia"
                                                postalAbbr:@"GA" designation:qeKeyUSStateDesigState fips52Numeric:@"13" fips52Alpha:@"GA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Guam"
                                                postalAbbr:@"GU" designation:qeKeyUSStateDesigTerritory fips52Numeric:@"66" fips52Alpha:@"GU"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Hawaii"
                                                postalAbbr:@"HI" designation:qeKeyUSStateDesigState fips52Numeric:@"15" fips52Alpha:@"HI"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Idaho"
                                                postalAbbr:@"ID" designation:qeKeyUSStateDesigState fips52Numeric:@"16" fips52Alpha:@"ID"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Illinois"
                                                postalAbbr:@"IL" designation:qeKeyUSStateDesigState fips52Numeric:@"17" fips52Alpha:@"IL"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Indiana"
                                                postalAbbr:@"IN" designation:qeKeyUSStateDesigState fips52Numeric:@"18" fips52Alpha:@"IN"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Iowa"
                                                postalAbbr:@"IA" designation:qeKeyUSStateDesigState fips52Numeric:@"19" fips52Alpha:@"IA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Kansas"
                                                postalAbbr:@"KS" designation:qeKeyUSStateDesigState fips52Numeric:@"20" fips52Alpha:@"KS"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Kentucky"
                                                postalAbbr:@"KY" designation:qeKeyUSStateDesigState fips52Numeric:@"21" fips52Alpha:@"KY"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Louisiana"
                                                postalAbbr:@"LA" designation:qeKeyUSStateDesigState fips52Numeric:@"22" fips52Alpha:@"LA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Maine"
                                                postalAbbr:@"ME" designation:qeKeyUSStateDesigState fips52Numeric:@"23" fips52Alpha:@"ME"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Marshall Islands" 
                                                postalAbbr:@"MH" designation:qeKeyUSStateDesigNation fips52Numeric:@"68" fips52Alpha:@"MH"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Maryland" 
                                                postalAbbr:@"MD" designation:qeKeyUSStateDesigState fips52Numeric:@"24" fips52Alpha:@"MD"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Massachusetts" 
                                                postalAbbr:@"MA" designation:qeKeyUSStateDesigState fips52Numeric:@"25" fips52Alpha:@"MA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Michigan" 
                                                postalAbbr:@"MI" designation:qeKeyUSStateDesigState fips52Numeric:@"26" fips52Alpha:@"MI"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Minnesota" 
                                                postalAbbr:@"MN" designation:qeKeyUSStateDesigState fips52Numeric:@"27" fips52Alpha:@"MN"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Mississippi" 
                                                postalAbbr:@"MS" designation:qeKeyUSStateDesigState fips52Numeric:@"28" fips52Alpha:@"MS"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Missouri" 
                                                postalAbbr:@"MO" designation:qeKeyUSStateDesigState fips52Numeric:@"29" fips52Alpha:@"MO"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Montana" 
                                                postalAbbr:@"MT" designation:qeKeyUSStateDesigState fips52Numeric:@"30" fips52Alpha:@"MT"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Nebraska" 
                                                postalAbbr:@"NE" designation:qeKeyUSStateDesigState fips52Numeric:@"31" fips52Alpha:@"NE"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Nevada" 
                                                postalAbbr:@"NV" designation:qeKeyUSStateDesigState fips52Numeric:@"32" fips52Alpha:@"NV"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"New Hampshire"
                                                postalAbbr:@"NH" designation:qeKeyUSStateDesigState fips52Numeric:@"33" fips52Alpha:@"NH"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"New Jersey" 
                                                postalAbbr:@"NJ" designation:qeKeyUSStateDesigState fips52Numeric:@"34" fips52Alpha:@"NJ"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"New Mexico" 
                                                postalAbbr:@"NM" designation:qeKeyUSStateDesigState fips52Numeric:@"35" fips52Alpha:@"NM"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"New York" 
                                                postalAbbr:@"NY" designation:qeKeyUSStateDesigState fips52Numeric:@"36" fips52Alpha:@"NY"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"North Carolina" 
                                                postalAbbr:@"NC" designation:qeKeyUSStateDesigState fips52Numeric:@"37" fips52Alpha:@"NC"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"North Dakota" 
                                                postalAbbr:@"ND" designation:qeKeyUSStateDesigState fips52Numeric:@"38" fips52Alpha:@"ND"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Northern Mariana Islands" 
                                                postalAbbr:@"MP" designation:qeKeyUSStateDesigTerritory fips52Numeric:@"69" fips52Alpha:@"MP"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Ohio"
                                                postalAbbr:@"OH" designation:qeKeyUSStateDesigState fips52Numeric:@"39" fips52Alpha:@"OH"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Oklahoma" 
                                                postalAbbr:@"OK" designation:qeKeyUSStateDesigState fips52Numeric:@"40" fips52Alpha:@"OK"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Oregon" 
                                                postalAbbr:@"OR" designation:qeKeyUSStateDesigState fips52Numeric:@"41" fips52Alpha:@"OR"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Palau" 
                                                postalAbbr:@"PW" designation:qeKeyUSStateDesigTrust fips52Numeric:@"70" fips52Alpha:@"PW"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Pennsylvania" 
                                                postalAbbr:@"PA" designation:qeKeyUSStateDesigState fips52Numeric:@"42" fips52Alpha:@"PA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Rhode Island" 
                                                postalAbbr:@"RI" designation:qeKeyUSStateDesigState fips52Numeric:@"44" fips52Alpha:@"RI"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"South Carolina" 
                                                postalAbbr:@"SC" designation:qeKeyUSStateDesigState fips52Numeric:@"45" fips52Alpha:@"SC"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"South Dakota" 
                                                postalAbbr:@"SD" designation:qeKeyUSStateDesigState fips52Numeric:@"46" fips52Alpha:@"SD"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Tennessee" 
                                                postalAbbr:@"TN" designation:qeKeyUSStateDesigState fips52Numeric:@"47" fips52Alpha:@"TN"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Texas" 
                                                postalAbbr:@"TX" designation:qeKeyUSStateDesigState fips52Numeric:@"48" fips52Alpha:@"TX"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Utah" 
                                                postalAbbr:@"UT" designation:qeKeyUSStateDesigState fips52Numeric:@"49" fips52Alpha:@"UT"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Vermont" 
                                                postalAbbr:@"VT" designation:qeKeyUSStateDesigState fips52Numeric:@"50" fips52Alpha:@"VT"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Virgin Islands" 
                                                postalAbbr:@"VI" designation:qeKeyUSStateDesigTerritory fips52Numeric:@"78" fips52Alpha:@"VI"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Virginia" 
                                                postalAbbr:@"VA" designation:qeKeyUSStateDesigState fips52Numeric:@"51" fips52Alpha:@"VA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Washington" 
                                                postalAbbr:@"WA" designation:qeKeyUSStateDesigState fips52Numeric:@"53" fips52Alpha:@"WA"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"West Virginia" 
                                                postalAbbr:@"WV" designation:qeKeyUSStateDesigState fips52Numeric:@"54" fips52Alpha:@"WV"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Wisconsin" 
                                                postalAbbr:@"WI" designation:qeKeyUSStateDesigState fips52Numeric:@"55" fips52Alpha:@"WI"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Wyoming" 
                                                postalAbbr:@"WY" designation:qeKeyUSStateDesigState fips52Numeric:@"56" fips52Alpha:@"WY"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Puerto Rico" 
                                                postalAbbr:@"PR" designation:qeKeyUSStateDesigTerritory fips52Numeric:@"72" fips52Alpha:@"PR"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"U.S. Minor Outlying Islands" postalAbbr:nil designation:qeKeyUSStateDesigTerritory fips52Numeric:@"74" fips52Alpha:@"UM"] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Baker Island" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"81" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Howland Island" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"84" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Jarvis Island" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"86" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Johnston Atoll" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"67" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Kingman Reef" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"89" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Midway Islands" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"71" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Navassa Island" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"76" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Palmyra Atoll" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"95" fips52Alpha:nil] autorelease]];
        [states addObject:[[[QEStateUS alloc] initWithName:@"Wake Island" postalAbbr:nil designation:qeKeyUSStateDesigMinorI fips52Numeric:@"79" fips52Alpha:nil] autorelease]];
        
        allStates = [[NSArray alloc] initWithArray:states];
    } // if
} // initialize


+ (NSArray *)allStates
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return allStates;
} // allStates


- (id)init
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithName:nil postalAbbr:nil designation:nil fips52Numeric:nil fips52Alpha:nil iso3166_2:nil];
} // init


- (id)initWithName:(NSString *)newName
        postalAbbr:(NSString *)newPostalAbbr
       designation:(NSString *)newDesignation
     fips52Numeric:(NSString *)newFips52Numeric
       fips52Alpha:(NSString *)newFips52Alpha
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    NSString *newIso3166_2 = nil;
    if (nil != [self fips52Alpha]) {
        newIso3166_2 = [NSString stringWithFormat:@"US-%@", [self fips52Alpha]];
    } // if
    
    return [self initWithName:newName
                   postalAbbr:newPostalAbbr
                  designation:newDesignation
                fips52Numeric:newFips52Numeric
                  fips52Alpha:newFips52Alpha
                    iso3166_2:newIso3166_2];
} // initWithName:postalAbbr:designation:fips52Numeric:fips52Alpha:


- (id)initWithName:(NSString *)newName
        postalAbbr:(NSString *)newPostalAbbr
       designation:(NSString *)newDesignation
     fips52Numeric:(NSString *)newFips52Numeric
       fips52Alpha:(NSString *)newFips52Alpha
         iso3166_2:(NSString *)newIso3166_2
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    self = [super initWithName:newName
                   designation:newDesignation
                     iso3166_2:newIso3166_2
                          fips:newFips52Alpha
                    postalAbbr:newPostalAbbr
                    divisionOf:[QECountry countryForName:@"United States"]];
    if (nil != self) {
        fips52Numeric = [newFips52Numeric copy];
    } // if
    
    return self;
} // initWithName:postalAbbr:designation:fips52Numeric:fips52Alpha:iso3166_2:


- (NSString *)fips52Numeric;
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[fips52Numeric retain] autorelease];
} // fips52Numeric

- (NSString *)fips52Alpha;
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [super fips];
} // fips52Alpha


- (void)dealloc
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    [fips52Numeric release];
    
    [super dealloc];
} // dealloc

- (id)copyWithZone:(NSZone *)zone
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    QEStateUS *newData = [[[self class] allocWithZone:zone]
                          initWithName:[self name] 
                          postalAbbr:[self postalAbbr] 
                          designation:[self designation] 
                          fips52Numeric:[self fips52Numeric] 
                          fips52Alpha:[self fips52Alpha]
                          iso3166_2:[self iso3166_2]];
    return newData;
} // copyWithZone:

- (unsigned int)hash
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static const unsigned int HASH_FACTOR = 37;
    
    unsigned int result = [super hash];
    result = (result * HASH_FACTOR) + [[self fips52Numeric] hash];
    
    return result;
} // hash

- (BOOL)isEqual:(id)anObject
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return ([super isEqual:anObject] &&
            [[self fips52Numeric] isEqual:[anObject fips52Numeric]]);
} // isEqual:
@end
